//
//  NIMInputAudioRecordIndicatorView.m
//  NIMKit
//
//  Created by chris.
//  Copyright (c) 2015年 NetEase. All rights reserved.
//

// THOMAS

#import "NIMInputAudioRecordIndicatorView.h"
#import "UIImage+NIM.h"
#import "LFTMCCountDownView.h"
#import "NTESGlobalMacro.h"

#define NIMKit_ViewWidth 176
#define NIMKit_ViewHeight 140

#define LFTMC_Radius 10

#define NIMKit_TimeFontSize 20
#define NIMKit_TipFontSize 15

@interface NIMInputAudioRecordIndicatorView()
<LFTMCCountDownViewDelegate>
{
    UIImageView *_backgrounView;
    UIImageView *_tipBackgroundView;
}

@property (nonatomic, strong) UILabel *timeLabel;

@property (nonatomic, strong) UILabel *tipLabel;

@property (nonatomic, strong) LFTMCCountDownView *countDown;

@property (nonatomic, assign) NSTimeInterval lastTimeInterval;

@end

@implementation NIMInputAudioRecordIndicatorView
- (instancetype)init {
    self = [super init];
    if(self) {
        self.frame = CGRectMake(0, 0, NIMKit_ViewWidth, NIMKit_ViewHeight);
        self.backgroundColor = [UIColor whiteColor];
        
        self.layer.cornerRadius = LFTMC_Radius;
        self.layer.shadowOpacity = 0.8f;
        self.layer.shadowColor = [UIColor lightGrayColor].CGColor;
        self.layer.shadowOffset = CGSizeMake(2,2);
        self.layer.shadowRadius = 2;
        
        _countDown = [[LFTMCCountDownView alloc] initWithFrame:CGRectZero];
        _countDown.backgroundColor = self.backgroundColor;
        _countDown.lineWidth = 3.f;
        _countDown.trailLineColor = [UIColor lightGrayColor];
        _countDown.labelFont = [UIFont boldSystemFontOfSize:NIMKit_TimeFontSize];
        _countDown.labelTextColor = UIColorFromRGB(0X333333);
        _countDown.finishingText = @"0s";
        _countDown.lineColor = UIColorFromRGB(0x97CC00);
        _countDown.bgdColor = self.backgroundColor;
        _countDown.deadTime = 10;
        [self addSubview:_countDown];
        
        _tipLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        _tipLabel.font = [UIFont systemFontOfSize:NIMKit_TipFontSize];
        _tipLabel.textColor = [UIColor blackColor];
        _tipLabel.textAlignment = NSTextAlignmentCenter;
        _tipLabel.text = @"手指上滑，取消发送";
        [self addSubview:_tipLabel];
        
        self.phase = AudioRecordPhaseEnd;
        _lastTimeInterval = 0;
    }
    return self;
}

- (void)setRecordTime:(NSTimeInterval)recordTime {
    if (recordTime - _lastTimeInterval > 1) {
        _lastTimeInterval  = recordTime;
        [_countDown correct:_lastTimeInterval];
    }
}

- (void)setPhase:(NIMAudioRecordPhase)phase {
    if(phase == AudioRecordPhaseStart) {
        [_countDown start:60 interval:1];
//        [_countDown start:10];
    } else if(phase == AudioRecordPhaseCancelling) {
        _tipLabel.text = @"松开手指，取消发送";
        _tipLabel.textColor = [UIColor redColor];
    } else if (phase == AudioRecordPhaseRecording) {
        _tipLabel.text = @"手指上滑，取消发送";
        _tipLabel.textColor = [UIColor blackColor];
    } else {
//        NSLog(@"END");
//        [_countDown resetSelf];
    }
}

- (void)layoutSubviews {
    CGSize size = [_tipLabel sizeThatFits:CGSizeMake(NIMKit_ViewWidth, MAXFLOAT)];
    _tipLabel.frame = CGRectMake(0, NIMKit_ViewHeight - 10 - size.height, NIMKit_ViewWidth, size.height);
    
    CGFloat countDownHeight = 60;
    CGFloat countDownWidth  = 60;
    CGFloat x = NIMKit_ViewWidth / 2 - countDownWidth / 2;
    CGFloat y = (NIMKit_ViewHeight-size.height) / 2 - countDownHeight / 2;
    _countDown.frame = CGRectMake(x, y, countDownWidth, countDownHeight);
    
    
}


@end
